﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;

namespace QLBH.Danhmuc
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {

        private FrmDVT frmDVT;
        private bool isLogouting = false;

        public FrmMain()
        {
            InitializeComponent();
        }

        #region Hàm xử lý chung
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!isLogouting)
                {
                    IVTQLBH.UTILITIES.Utility.SaveStyle();
                    Application.Exit();
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// Tạo thêm tab mới
        /// </summary>
        /// <param name="tabControl">Tên TabControl để thêm tabpage mới vào</param>
        /// <param name="name">Tên tabpage mới</param>
        protected void TabCreating(XtraTabControl tabControl, string name, UserControl uc)
        {
            int index = KiemTraTabpageTonTai(tabControl, name);
            if (index >= 0)
            {
                tabControl.TabPages[index].Controls.Clear();
                uc.Parent = tabControl.TabPages[index];
                uc.Show();
                uc.Dock = DockStyle.Fill;

                if (tabControl.TabPages[index].PageVisible == false)
                    tabControl.TabPages[index].PageVisible = true;
                tabControl.SelectedTabPage = tabControl.TabPages[index];
            }
            else
            {
                XtraTabPage tabpage = new XtraTabPage { Text = name, Name = name, PageVisible = false };
                tabControl.TabPages.Add(tabpage);

                uc.Parent = tabpage;
                uc.Show();
                uc.Dock = DockStyle.Fill;

                tabpage.PageVisible = true;
                tabControl.SelectedTabPage = tabpage;
            }
        }

        //ducnm 22/07/2013
        /// <summary>
        /// Thêm form vào tab
        /// </summary>
        /// <param name="tabControl">tab?</param>
        /// <param name="name">tên?</param>
        /// <param name="frm">form?</param>
        protected void TabCreating(XtraTabControl tabControl, string name, Form frm)
        {
            int index = KiemTraTabpageTonTai(tabControl, name);
            if (index >= 0)
            {
                frm.Show();
                (frm as iFormBaseList).MeShow();

                if (tabControl.TabPages[index].PageVisible == false)
                    tabControl.TabPages[index].PageVisible = true;
                tabControl.SelectedTabPage = tabControl.TabPages[index];
            }
            else
            {
                XtraTabPage tabpage = new XtraTabPage { Text = name, Name = name, PageVisible = false };
                tabControl.TabPages.Add(tabpage);

                frm.FormBorderStyle = FormBorderStyle.None;
                frm.Dock = DockStyle.Fill;
                frm.TopLevel = false;
                tabpage.Controls.Add(frm);
                frm.Show();

                tabpage.PageVisible = true;
                tabControl.SelectedTabPage = tabpage;
            }
        }

        /// <summary>
        /// Kiểm tra tabpage có tồn tại hay không
        /// </summary>
        /// <param name="tabControlName">Tên TabControl để kiểm tra</param>
        /// <param name="tabName">Tên tabpage cần kiểm tra</param>
        protected int KiemTraTabpageTonTai(XtraTabControl tabControlName, string tabName)
        {
            int result = -1;
            for (int i = 0; i < tabControlName.TabPages.Count; i++)
            {
                if (tabControlName.TabPages[i].Name == tabName)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            try
            {
                XtraTabControl tabControl = sender as XtraTabControl;
                ClosePageButtonEventArgs arg = e as ClosePageButtonEventArgs;
                (arg.Page as XtraTabPage).PageVisible = false;
            }
            catch (Exception ex)
            {
                ;
            }
        }

        private void btnKetThuc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                //IVTQLBH.UTILITIES.Utility.SaveStyle();
                //Application.Exit();

                //ducnm: làm thế này ko giải phóng bộ nhớ -> chiếm dụng nhiều RAM
                //this.Hide();
                //frmDangNhap frmLogin = new frmDangNhap();
                //frmLogin.ShowDialog();

                isLogouting = true;
                this.Close();
            }
            catch (Exception)
            {
                ;
            }

        }
        #endregion

        //ducnm 13-08-2013
        /// <summary>
        /// hiện form từ từ ko đơ nữa
        /// </summary>
        #region show form
        private DevExpress.Utils.WaitDialogForm waitDialog = new DevExpress.Utils.WaitDialogForm("Đang nạp dữ liệu", "Xin vui lòng chờ...");
        private Queue<string> ShowFormKeys = new Queue<string>();
        private void ShowForm(string key)
        {
            waitDialog.Caption = "Đang nạp dữ liệu " + key;
            waitDialog.Show();
            ShowFormKeys.Enqueue(key);
            if (!bsShowForm.IsBusy)
                bsShowForm.RunWorkerAsync();
        }

        private void bsShowForm_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            while (ShowFormKeys.Count > 0)
            {
                string ShowFormKey = ShowFormKeys.Dequeue();
                switch (ShowFormKey)
                {
                    case "Đơn vị tính":
                        if (frmDVT == null || frmDVT.IsDisposed)
                            frmDVT = new FrmDVT();
                        bsShowForm.ReportProgress(1, ShowFormKey);
                        break;
                    case "aaaaaaaaaaa":
                        break;
                }
            }
        }
        private void bsShowForm_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                string ShowFormKey = e.UserState.ToString();
                waitDialog.Caption = "Hiển thị " + ShowFormKey;

                switch (ShowFormKey)
                {
                    case "Đơn vị tính":
                        TabCreating(xtraTabControl1, "Đơn vị tính", frmDVT);
                        break;
                    case "aaaaaaaaaaa":
                        break;
                }
            }
        }
        private void bsShowForm_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            waitDialog.Hide();
        }

        #endregion

        private void btnDVT_ItemClick(object sender, ItemClickEventArgs e)
        {
            //FrmDVT objDvt = new FrmDVT();
            //objDvt.Show();
            ShowForm("Đơn vị tính");
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                waitDialog.Hide();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}